from urllib import request,parse
import time,random,sys
from fake_useragent import UserAgent
class TiebaSpider():
    def __init__(self):
        self.url='http://tieba.baidu.com/f?{}'
    def get_html(self,url):
        req=request.Request(url=url,headers={'User-Agent':UserAgent.chrome})
        response=request.urlopen(req)
        html=response.read().decode('utf-8')
        return html
    def parse_html(self):
        pass
    def save_html(self,filename,html):
        with open(filename,'w') as f:
            f.write(html)
    def run(self):
        name=input('Enter a name: ')
        begin=int(input('Enter the start page: '))
        stop=int(input('Enter the end page: '))
        for page in range(begin,stop+1):
            pn=(page-1)*50
            params={'kw':name,'pn':str(pn)}
            url=self.url.format(parse.urlencode(params))
            html=self.get_html(url)
            filename='{}-{}P.html'.format(name,page)
            self.save_html(filename,html)
            sys.stdout.write('Page %d'%page)
            time.sleep(random.randint(1,2))
if __name__ == '__main__':
    start=time.time()
    spider=TiebaSpider()
    spider.run()
    end=time.time()
    print('Time:%.2f'%(end-start))
